﻿using DSPL.Common.Classes;
using DSPL.Common.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DSPL.Common.CommonControls.Controls
{
    /// <summary>
    /// Interaction logic for DataGridCellTemplateControl.xaml
    /// </summary>
    public partial class DataGridCellTemplateControl : UserControl
    {
        public DataGridCellTemplateControl()
        {
            InitializeComponent();
        }

        public object Value
        {
            get { return (object)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(object), typeof(DataGridCellTemplateControl), new PropertyMetadata(null));

        private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DataGridCellTemplateControl control = d as DataGridCellTemplateControl;

            if (control != null)
            {
                ColumnValueProperties selectedFilterKey = control.SelectedColumnValue;

                if (selectedFilterKey != null)
                {
                    switch (selectedFilterKey.DataType)
                    {
                        case "INT16":
                        case "INT32":
                        case "INT64":
                        case "DECIMAL":
                            control.textBox.Text = e.NewValue.ToString();
                            break;
                        case "DATETIME":
                            control.datePicker.SelectedDate = DateTime.Parse(e.NewValue.ToString());
                            break;

                        default:
                            break;
                    }
                }
            }
        }



        public ColumnValueProperties SelectedColumnValue
        {
            get { return (ColumnValueProperties)GetValue(SelectedColumnValueProperty); }
            set { SetValue(SelectedColumnValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedColumnVaue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedColumnValueProperty =
            DependencyProperty.Register("SelectedColumnValue", typeof(ColumnValueProperties), typeof(DataGridCellTemplateControl), new PropertyMetadata(null, SelectedColumnValueChanged));

        private static void SelectedColumnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DataGridCellTemplateControl control = d as DataGridCellTemplateControl;

            if (control != null)
            {
                ColumnValueProperties selectedFilterKey = e.NewValue as ColumnValueProperties;

                if (selectedFilterKey != null)
                {
                    switch (selectedFilterKey.DataType)
                    {
                        case "INT16":
                        case "INT32":
                        case "INT64":
                        case "DECIMAL":
                            control.textBox.Visibility = Visibility.Visible;
                            control.datePicker.Visibility = Visibility.Collapsed;
                            break;
                        case "DATETIME":
                            control.textBox.Visibility = Visibility.Collapsed;
                            control.datePicker.Visibility = Visibility.Visible;
                            break;

                        default:
                            break;
                    }
                }
            }
        }

        private void textBox_PreviewTextInput_1(object sender, TextCompositionEventArgs e)
        {
            if (SelectedColumnValue.DataType == "INT16")
            {
                Int16 filterValue = 0;
                if (!Int16.TryParse(textBox.Text + e.Text, out filterValue))
                {
                    e.Handled = true;
                }

            }
            else if (SelectedColumnValue.DataType == "INT32")
            {
                Int32 filterValue = 0;
                if (!Int32.TryParse(textBox.Text + e.Text, out filterValue))
                {
                    e.Handled = true;
                }
            }
            else if (SelectedColumnValue.DataType == "INT64")
            {
                Int64 filterValue = 0;
                if (!Int64.TryParse(textBox.Text + e.Text, out filterValue))
                {
                    e.Handled = true;
                }
            }
            else if (SelectedColumnValue.DataType == "DECIMAL")
            {
                if (e.Text.Equals(".") && !textBox.Text.Contains("."))
                {
                    e.Handled = false;
                    return;
                }
                Decimal filterValue = 0;
                if (!Decimal.TryParse(textBox.Text + e.Text, out filterValue))
                {
                    e.Handled = true;
                }
            }
            else if (SelectedColumnValue.DataType == "DATETIME")
            {

            }
        }

        private void textBox_TextChanged_1(object sender, TextChangedEventArgs e)
        {
            this.Value = textBox.Text;
        }

        private void datePicker_SelectedDateChanged_1(object sender, SelectionChangedEventArgs e)
        {
            this.Value = datePicker.SelectedDate.Value.ToShortDateString();
            //this.Value = datePicker.SelectedDate.Value.Day + "/" + datePicker.SelectedDate.Value.Month + "/" + datePicker.SelectedDate.Value.Year;
        }
    }
}
